package com.iuie.ns.datum.dao.impl;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.iuie.ns.datum.dao.IBlogDao;
import com.iuie.ns.datum.vo.BlogInfoVO;
import com.iuie.ns.frame.persistence.dao.impl.GenericDaoImpl;
import com.iuie.ns.frame.utils.StringUtils;

@Repository("blogDao")
public class BlogDaoImpl extends GenericDaoImpl<BlogInfoVO> implements IBlogDao {

	@Override
	public void updateBlog(BlogInfoVO blogInfo) {
		Query query = getCurrentSession().createQuery("update BlogInfoVO set blogName=:blogName, createTime=:createTime, author=:author, "
				+ "authorId=:authorId, visits=:visits, remarks=:remarks, link=:link, updateTime=:updateTime where blogId=:blogId");
		query.setString("blogName", blogInfo.getBlogName());
		query.setTimestamp("createTime", blogInfo.getCreateTime());
		query.setString("author", blogInfo.getAuthor());
		query.setString("authorId", blogInfo.getAuthorId());
		query.setInteger("visits", blogInfo.getVisits());
		query.setString("remarks", blogInfo.getRemarks());
		query.setString("link", blogInfo.getLink());
		query.setLong("blogId", blogInfo.getBlogId());
		query.setTimestamp("updateTime", blogInfo.getUpdateTime());
		query.executeUpdate();
		
		Query queryBlogType = getCurrentSession().createSQLQuery("update ns_blog_info set blog_type=:blogType where blog_id=:blogId");
		queryBlogType.setString("blogType", blogInfo.getBlogType().getCodeInfoValue());
		queryBlogType.setLong("blogId", blogInfo.getBlogId());
		queryBlogType.executeUpdate();
		
		if (StringUtils.isNotBlank(blogInfo.getLink())) {
			// 超链接类型的博客，需要更新文件信息
			Query queryFileInfo = getCurrentSession().createQuery("update FileInfoVO set fileName=:fileName where fileId=:fileId");
			queryFileInfo.setString("fileName", blogInfo.getLink());
			queryFileInfo.setLong("fileId", blogInfo.getFileInfo().getFileId());
			queryFileInfo.executeUpdate();
		}
	}

	@Override
	public void addBlog(BlogInfoVO blogInfo) {
		Query query = getCurrentSession().createSQLQuery("insert into ns_blog_info(blog_id,author,blog_name,create_time,link,remarks,visits,blog_type,author_id,blog_attr,md_content,update_time)"
				+ "values(:blogId,:author,:blogName,:createTime,:link,:remarks,:visits,:blogType,:authorId,:blogAttr,:mdContent,:updateTime)");
		query.setLong("blogId", blogInfo.getBlogId());
		query.setString("author", blogInfo.getAuthor());
		query.setString("blogName", blogInfo.getBlogName());
		query.setTimestamp("createTime", blogInfo.getCreateTime());
		query.setString("link", blogInfo.getLink());
		query.setString("remarks", blogInfo.getRemarks());
		query.setInteger("visits", blogInfo.getVisits());
		query.setString("blogType", blogInfo.getBlogType().getCodeInfoValue());
		query.setString("authorId", blogInfo.getAuthorId());
		query.setString("blogAttr", blogInfo.getBlogAttr());
		query.setText("mdContent", blogInfo.getMdContent());
		query.setTimestamp("updateTime", blogInfo.getUpdateTime());
		query.executeUpdate();
		
		Query queryFileInfo = getCurrentSession().createQuery("update BlogInfoVO set fileInfo=:fileInfo where blogId=:blogId");
		queryFileInfo.setLong("fileInfo", blogInfo.getFileInfo().getFileId());
		queryFileInfo.setLong("blogId", blogInfo.getBlogId());
		queryFileInfo.executeUpdate();
	}

}
